package com.baizhi.cmfz.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.cmfz.entity.CmfzBanner;
import com.baizhi.cmfz.entity.CmfzGuru;
import com.baizhi.cmfz.mapper.CmfzBannerMapper;
import com.baizhi.cmfz.service.CmfzBannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/banner")
public class CmfzBannerController {
    @Autowired
    private CmfzBannerService cbs;
    @Autowired
    private CmfzBannerMapper cbm;

    /**
     * 展示所有轮播图信息
     */
    @RequestMapping("/selectAllBanner")
    @ResponseBody
    public Map<String,Object> selectAllBanner(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                              @RequestParam(value = "limit",defaultValue = "5") Integer limit){

        //调用serive层写好的分页查询方法
        Page<CmfzBanner> pi = cbs.selectAllByPage(page, limit);
        //改好layui需要的json格式
        Map<String, Object> map = new HashMap<>();
        //R对象的无参构造这俩值已经默认赋值过
        map.put("code",0);
        map.put("msg","");
        map.put("count",pi.getTotal());
        map.put("data",pi.getRecords());
        return map;
    }


    /**
     * 上传轮播图图片 此处不再使用虚拟路径 直接上传到项目中去
     */


    /**
     * 添加banner对象
     */
    @RequestMapping("/addBanner")
    @ResponseBody
    public void addBanner(CmfzBanner b){
        cbs.insertBanner(b);
    }

    /**
     * 假删除 在前台页面上不展示 修改展示状态
     */
    @RequestMapping("/updateState")
    @ResponseBody
    public void updateState(CmfzBanner b){
        cbs.updateStatus(b);
    }

    /**
     * 导出数据
     */
    @RequestMapping("/export")
    @ResponseBody
    public void exportBanner(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //准备数据
        List<CmfzBanner> bannerList = cbm.selectList(null);
        //导出参数对象 标题 表名
        ExportParams exportParams = new ExportParams("轮播图数据", "banners");
        //调用导出文件的方法
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, CmfzBanner.class, bannerList);
        //保存
        //FileOutputStream fileOutputStream = new FileOutputStream("C://Users//huimao//Desktop//上师.xls");
        //workbook.write(fileOutputStream);
        //利用浏览器下载
        response.setHeader("content-disposition","attachment;fileName="+ URLEncoder.encode("banner.xls","utf-8"));
        workbook.write(response.getOutputStream());
    }

    /**
     * 导入数据
     */
    @RequestMapping("/import")
    @ResponseBody
    public Map importBanner(MultipartFile file, HttpServletRequest request) throws Exception {
        //获取流
        InputStream inputStream = file.getInputStream();
        //设置标题所占的行数 表头所占的行数
        ImportParams importParams = new ImportParams();
        importParams.setHeadRows(1);
        importParams.setTitleRows(1);

        Map<String, Object> map = new HashMap<>();
        //获取excle中的数据然后封装到集合中去
        try {
            List<CmfzBanner> list = ExcelImportUtil.importExcel(inputStream, CmfzBanner.class, importParams);
            for (CmfzBanner cmfzBanner : list) {
                System.out.println(cmfzBanner);
                cbs.insertBanner(cmfzBanner);

            }
            map.put("msg",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg",false);
        }
        return map;

    }


}
